package com.baidu.demo;

import cn.hutool.json.JSONUtil;
import com.baidu.entity.User;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//将java对象转换为json字符串
public class Demo02 {
    public static void main(String[] args) {
        User user = new User();
        user.setId(1);
        user.setUsername("zhangsan");
        user.setCreatedAt(LocalDateTime.now());

        User user2 = new User();
        user2.setId(1);
        user2.setUsername("zhangsan");
        user2.setCreatedAt(LocalDateTime.now());

        User user3 = new User();
        user3.setId(1);
        user3.setUsername("zhangsan");
        user3.setCreatedAt(LocalDateTime.now());
        //把它装进一个集合里
        List<User> userList = new ArrayList<>();
        userList.add(user);
        userList.add(user2);
        userList.add(user3);
        //map
        Map<String, Object> map = new HashMap<>();
        map.put("name", "zhangsan");
        map.put("age", 20);
        String jsonStr1 = JSONUtil.toJsonStr(map);
        System.out.println(jsonStr1);


         //将user对象转换为json字符串
        String jsonStr = JSONUtil.toJsonStr(userList);

        System.out.println(jsonStr);
    }
}
